package com.yunhui.controller;

import com.aliyuncs.exceptions.ClientException;
import com.yunhui.entity.RouteJb;
import com.yunhui.entity.User;
import com.yunhui.mapper.UserMapper;
import com.yunhui.service.RouteJbService;
import com.yunhui.service.UserService;
import com.yunhui.util.PageSupport;
import com.yunhui.util.SmsUtil;
import com.yunhui.util.UserImgFile;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 用户的前端控制
 */
@Controller("user")
public class userController {
    @Resource
   UserMapper userMapper ;

    @Resource
    private UserService userService;
    @Resource
    private RouteJbService routeJbService;
    @RequestMapping("/")
    public String index(Model model) {
        System.err.println(123);
        RouteJb routeJb=new RouteJb();
        routeJb.setStatus(2);
        PageSupport page=new PageSupport();
        page.setPageSize(2);
        page.setCurrentPageNo(1);
        List<RouteJb> list=routeJbService.getPageRouteJb(routeJb,page);
        model.addAttribute("list",list);
        return "index";
    }
    @RequestMapping("houtai")
    public String index2(){
        return "/houtai/index";
    }
    @RequestMapping("houtat1")
    public String index3(){
        //System.out.println("saowen");
        return "/houtai/welcome";
    }
    @RequestMapping("houtat2")
    public String index4(){
        return "pack";
    }

    @RequestMapping("login_sign")
    public String login_sign() {
        return "/login/login_sign";
    }
 

    //登录验证
    @RequestMapping(value = "get_login")
    @ResponseBody
    public String get_login(@RequestParam("phone_email") String phone_email,
                            @RequestParam("password") String password,
                            HttpSession session) {



        User loguser = userService.selectByPhoneOrEmail(phone_email);
        if (loguser == null) {

            return "对不起！该用户不存在";
        }
        if (password.equals(loguser.getPassword())) {
            session.setAttribute("LoginUser", loguser);
            return "index";
        } else {

            return "密码错误";
        }

    }
    //忘记密码
    @RequestMapping("forpwd")
    public String forpwd(){
        return "/login/forpwd";
    }

    //注册用户
    @RequestMapping("save_user")
    public String save_user(String phone,String pwd,String email, HttpSession session) {

        User user = new User();
        user.setPhone(phone);
        user.setEmail(email);
        user.setPassword(pwd);
        user.setCreateDate(new Date());
        user.setImgurl("../img/user_login_sign/dd.jpg");
        user.setImgname("dd");
        user.setState(1);
        user.setName("cdd");
        user.setBirthday(new Date());
        if(userService.save(user)){
            session.setAttribute("LoginUser", user);
            return "index";
        }


        return "/login/login_sign";
    }
    //注册ajax手机号及邮箱验证
    @RequestMapping("emailOrphone")
    @ResponseBody
    public String emailOrphone(String email,String phone){
        String email_phone = "yes";

        if(email!=null&&email != "null"&&userService.selectByPhoneOrEmail(email)!=null){
            email_phone = "email";
        }

        if(phone!=null&&phone != "null"&&userService.selectByPhoneOrEmail(phone)!=null){
            email_phone = "phone";
        }



        return email_phone;
    }
    SmsUtil smsUtil = new SmsUtil();
    //获取验证码
    @RequestMapping("phoneCode")
    @ResponseBody
    public String phoneCode(@RequestParam("phone") String phone) throws ClientException {
        System.err.println(phone);
        //随机6位数验证码
        Integer code =  (int) ((Math.random() * 9 + 1) * 100000);
        if(smsUtil.sendSms(phone,code)==200){
            System.err.println(code);
            return code.toString();
        }


        return "1234";
    }


    PageSupport page = new PageSupport();
    //后台管理
    @RequestMapping("user_list")
    public String user_list(String begin,String end,String phone_email,String index,Model model){
        Integer indext = 1;

        if(index!=null){
            indext = Integer.parseInt(index);
        }

        if(begin!=null){
            if(begin.equals("null")){
                begin = null;
            }
        }
        if(end!=null){
            if(end.equals("null")){
                end = null;
            }
        }
        if(phone_email!=null){
            if(phone_email.equals("null")){
                phone_email = null;
            }
        }



        List<User> list = userService.listByPage(indext,page.getPageSize(),begin,end,phone_email);
        page.setTotalCount(userService.countByPage(begin,end,phone_email));
        page.setCurrentPageNo(indext);

        model.addAttribute("userlist",list);
        model.addAttribute("page",page);
        model.addAttribute("begindate",begin);
        model.addAttribute("enddate",end);
        model.addAttribute("phone_email",phone_email);

        return "/houtai/user_list";
    }
    //后台状态修改
    @RequestMapping("updateStates")
    public String updateStates(String upphone){

        User user =  userService.selectByPhoneOrEmail(upphone);
        if(user.getState()==1){
            user.setState(2);
        }else{
            user.setState(1);
        }

        userService.updateById(user);
        return "redirect:/user_list";
    }




    //订单个人信息查看及修改
    @RequestMapping("orderuser")
    public String orderuser(HttpSession session, Model model){
        return "information";
    }


    //修改个人信息
    @RequestMapping("updateUser")
    public String publicUser(User user,HttpSession session,Model model,String birthdaydate)  {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        if(birthdaydate!=null){
            System.out.println(birthdaydate);
            try {
                user.setBirthday(df.parse(birthdaydate));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        boolean bool = false;
        if(user !=null){
            user.setModify(new Date());
            bool =  userService.updateById(user);
        }

        if(bool){
            session.setAttribute("LoginUser",userService.selectByPhoneOrEmail(user.getEmail()));
        }




        return "information";
    }



    //修改头像
    @RequestMapping("updateHead")
    public String updateHead(MultipartFile fileup, HttpSession session){
        User user = (User) session.getAttribute("LoginUser");
        UserImgFile img = new UserImgFile();
        user = img.getName(fileup,user);

        if(userService.updateById(user)) {
        	return "information";
        }

        return "information";
    }

    //修改密码
    @RequestMapping("repassword")
    public String repassword(String phone,String password,HttpSession session){
        System.out.println(phone);
        System.out.println(password);
        User user = userService.selectByPhoneOrEmail(phone);
        user.setPassword(password);
        userService.updateById(user);
        return "/login/login_sign";
        
    }




}
